﻿using SharpDX.Direct3D11;
using System.Collections.Generic;
using System.IO;

namespace Engine.Graphics
{
    public class TextureManager
    {
        #region Private Fields

        private Engine engine;
        private Dictionary<string, int> GetTextureId;
        private string textureFolder;
        private List<ShaderResourceView> textures;

        #endregion Private Fields

        #region Public Constructors

        public TextureManager(Engine engine, string textureFolder)
        {
            this.engine = engine;
            this.textureFolder = textureFolder;
            this.GetTextureId = new Dictionary<string, int>();
            this.textures = new List<ShaderResourceView>();
            Ptx2bmpConverter.DDSFile dds = new Ptx2bmpConverter.DDSFile(8, 8, 1, Ptx2bmpConverter.DDSFile.DDSType.DXT5);

            using (Stream stream = (Stream)dds.ToStream())
            {
                stream.Position = 0;
                this.WhiteShaderResource = ShaderResourceView.FromStream(this.engine.Grafic.Device, stream, (int)stream.Length);
            }

            // Test if all Textures can be loaded:
            // TestPTXLoaderForAllTextureLoading();
        }

        #endregion Public Constructors

        #region Public Properties

        public string TextureFolder
        {
            get { return textureFolder; }
            set { textureFolder = value; }
        }

        public List<ShaderResourceView> Textures
        {
            get
            {
                return this.textures;
            }
        }

        // Public Methods (2) 

        public ShaderResourceView WhiteShaderResource { get; set; }

        #endregion Public Properties

        #region Public Methods

        /// <summary>
        /// Adds a new Texture to the manager.
        /// </summary>
        /// <param name="shortFilename">short Filename with extension (Fels.PTX)</param>
        /// <returns></returns>
        public int AddTexture(string shortFilename)
        {
            int id = 0;
            if (GetTextureId.TryGetValue(shortFilename, out id))
            {
                return id;
            }
            this.GetTextureId.Add(shortFilename, this.textures.Count);

            if (shortFilename != string.Empty)
                this.textures.Add(Ptx2bmpConverter.Ptx2bmp.ToDDSSlimDX(this.engine.Device, this.textureFolder + shortFilename));
            else
                this.textures.Add(null);

            return this.textures.Count - 1;
        }

        public string GetTextureName(int id)
        {
            foreach (var item in this.GetTextureId)
            {
                if (item.Value == id)
                    return item.Key;
            }
            return string.Empty;
        }

        #endregion Public Methods

        #region Private Methods

        private void TestPTXLoaderForAllTextureLoading()
        {
            var files = System.IO.Directory.GetFiles(System.Windows.Forms.Application.StartupPath + "/Texture", "*.ptx");

            for (int i = 0; i < files.Length; i++)
            {
                var texture = Ptx2bmpConverter.Ptx2bmp.ToDDSSlimDX(this.engine.Grafic.Device, files[i]);
            }
        }

        #endregion Private Methods
    }
}